package com.jiangxiaofei.citycloud.activity.fleamarket;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.blankj.utilcode.util.EmptyUtils;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.GoodIdBean;
import com.jiangxiaofei.citycloud.constants.Constants;
import com.jiangxiaofei.citycloud.constants.IntConstants;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.mvp.FleaConstant;
import com.jiangxiaofei.citycloud.mvp.FleaPersenter;
import com.jiangxiaofei.citycloud.param.PutNeedParam;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * @author: JiangXFei
 * @date: 2017/11/13 0013
 * @content: 物资需求发布页面
 */

public class FleaMarketNeedPublishActivity extends BaseActivity implements FleaConstant.PutNeedView {
    public static final int TYPERESULT = 0X0004;
    @InjectView(R.id.tv_good_type)
    TextView tvGoodType;
    @InjectView(R.id.linear_select_type)
    LinearLayout linearSelectType;
    @InjectView(R.id.et_good_price)
    EditText etGoodPrice;
    @InjectView(R.id.linear_select_price)
    LinearLayout linearSelectPrice;
    @InjectView(R.id.btn_publish)
    Button btnPublish;
    @InjectView(R.id.et_title)
    EditText etTitle;
    @InjectView(R.id.tv_description)
    EditText etDescription;
    private int typeId = -1;
    private PutNeedParam putNeedParam = new PutNeedParam();
    private FleaConstant.PutNeedPersenter putNeedPersenter;
    private BDLocation bdLocation;
    private int needId = 0;

    @Override
    protected void initView(Bundle savedInstanceState) {
        mActivity = this;
        mContext = this;
        setContentView(R.layout.activity_flea_need_publish_layout);
        ButterKnife.inject(this);
        initTitle();
        putNeedPersenter = new FleaPersenter(mContext, (FleaConstant.PutNeedView) mActivity);
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        setLeftOneBg(R.drawable.ic_back_black);
        setCTitle("发布需求");
        mCenterTitle.setTextColor(Color.parseColor("#7b7b7b"));
        rl_layout.setBackgroundColor(Color.parseColor("#ffffff"));
    }


    @OnClick({R.id.linear_select_type, R.id.btn_publish})
    public void bOnclick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.linear_select_type:
                Intent intent = new Intent(mContext, FleaMarketCassifyActivity.class);
                startActivityForResult(intent, TYPERESULT);
                break;
            case R.id.btn_publish:
                //需求发布
                if (isForm()) {
                    putNeedPersenter.putNeed();
                }
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case IntConstants.FLEARESPONSECODE:
                typeId = data.getIntExtra(StringConstants.FLEATYPEID, -1);
                String name = data.getStringExtra(StringConstants.FLEATYPE);
                if (EmptyUtils.isNotEmpty(name)) {
                    tvGoodType.setText(name);
                }
                if (typeId != -1) {

                }
                break;

        }
    }

    /**
     * 表单验证
     *
     * @return 表单验证成功返回TRUE
     */
    private boolean isForm() {
        String title = etTitle.getText().toString();
        String description = etDescription.getText().toString();
        String price = etGoodPrice.getText().toString();
        if (EmptyUtils.isEmpty(title)) {
            Toast.makeText(mContext, "请输入需求标题", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (EmptyUtils.isEmpty(description)) {
            Toast.makeText(mContext, "请输入描述信息", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (EmptyUtils.isEmpty(price)) {
            Toast.makeText(mContext, "请输入价格", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (typeId == -1) {
            Toast.makeText(mContext, "请选择分类", Toast.LENGTH_SHORT).show();
            return false;
        }
        putNeedParam.setAuthor(Constants.userId);
        putNeedParam.setCat(typeId);
        putNeedParam.setDescription(description);
        if (EmptyUtils.isNotEmpty(Constants.bdLocation)) {
            bdLocation = Constants.bdLocation;
        } else {
            Constants.bdclient.start();
            bdLocation = Constants.bdLocation;
        }
        putNeedParam.setLat(String.valueOf(bdLocation.getLatitude()));
        putNeedParam.setLng(String.valueOf(bdLocation.getLongitude()));
        putNeedParam.setTitle(title);
        putNeedParam.setPrice(price);
        return true;
    }

    @Override
    public void setData(Object o) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }

    @Override
    public PutNeedParam getPutNeedParam() {
        return putNeedParam;
    }


    /**
     * 发布成功
     */
    @Override
    public void putNeedSuccess(Object o) {
        Toast.makeText(mContext, "发布成功", Toast.LENGTH_SHORT).show();
        mActivity.finish();
        List<GoodIdBean> beans = (List<GoodIdBean>) o;
        if (beans.size() > 0) {
            needId = Integer.valueOf(beans.get(0).getId());
            Intent intent = new Intent(mContext, FleaMarketNeedDetailActivity.class);
            intent.putExtra(StringConstants.FLEANEEDID, needId);
            startActivity(intent);
        }
    }
}
